﻿namespace OP10.ViewModels
{
    public class ST04WeldTestViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;

        public ST04WeldTestViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            var QG3 = new CylinderModel("ST4压料上下气缸", 0, false, false);
            var QG4 = new CylinderModel("ST4电极上下气缸", 0, false, false);
            var QG5 = new CylinderModel("ST4工站脱离", 0, false, false);


            QGList.Add(QG3);
            QGList.Add(QG4);
            QGList.Add(QG5);


            //开启定时器任务
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(200);
            dispatcherTimer.Tick += OnTick;
            dispatcherTimer.Start();
        }

        private void OnTick(object? sender, EventArgs e)
        {
            //气缸状态
            foreach (var item in QGList)
            {

                switch (item.Name)
                {

                    case "ST4压料上下气缸":
                        item.QG_Status = ST04PushMaterialUpDownCy.cylinderPLCToPC_Global.nCyStatus;
                        item.bOriginSensor = ST04PushMaterialUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
                        item.bWorkSensor = ST04PushMaterialUpDownCy.cylinderPLCToPC_Global.bWorkSensor;
                        break;

                    case "ST4电极上下气缸":
                        item.QG_Status = ST04ElectrodeUpDownCy.cylinderPLCToPC_Global.nCyStatus;
                        item.bOriginSensor = ST04ElectrodeUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
                        item.bWorkSensor = ST04ElectrodeUpDownCy.cylinderPLCToPC_Global.bWorkSensor;
                        break;
                    case "ST4工站脱离":
                        item.QG_Status = ST04StationBreakAwayCy.cylinderPLCToPC_Global.nCyStatus;
                        item.bOriginSensor = ST04StationBreakAwayCy.cylinderPLCToPC_Global.bOriginSensor;
                        item.bWorkSensor = ST04StationBreakAwayCy.cylinderPLCToPC_Global.bWorkSensor;
                        break;
                    default:
                        break;
                }

            }

            LoadWireManStart = WirePLCToPC_Global.LoadWireManStart;
            WeldStart = WirePLCToPC_Global.WeldStart;

            LoadWireManStartText = LoadWireManStart ? "手动送桥丝启动中" : "手动送桥丝启动";
            WeldStartText = WeldStart ? "焊接启动中" : "焊接启动";

            TargetPos_X = WirePLCToPC_Global.TargetPos_X;
            TargetPos_YL = WirePLCToPC_Global.TargetPos_YL;
            TargetPos_YR = WirePLCToPC_Global.TargetPos_YR;

            WeldLength = WirePLCToPC_Global.WeldLength;
            ActualTension = WirePLCToPC_Global.ActualTension;
        }



        #region 事件
        private DelegateCommand _GoBackCommand;

        public DelegateCommand GoBackCommand =>
            _GoBackCommand ?? (_GoBackCommand = new DelegateCommand(ExecuteGoBackCommand));

        void ExecuteGoBackCommand()
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST04DeviceManagerView);
        }

        private DelegateCommand<string> _QGDownCommand;
        public DelegateCommand<string> QGDownCommand =>
            _QGDownCommand ?? (_QGDownCommand = new DelegateCommand<string>(ExecuteQGDownCommand));

        void ExecuteQGDownCommand(string parameter)
        {
            switch (parameter)
            {
                case "ST4压料上下气缸":
                    ST04PushMaterialUpDownCy.cylinderPCToPLC_Global.bManualBT = true;
                    break;

                case "ST4电极上下气缸":
                    ST04ElectrodeUpDownCy.cylinderPCToPLC_Global.bManualBT = true;
                    break;
                case "ST4工站脱离":
                    ST04StationBreakAwayCy.cylinderPCToPLC_Global.bManualBT = true;
                    break;

                default:
                    break;
            }
        }


        private DelegateCommand<string> _QGUpCommand;
        public DelegateCommand<string> QGUpCommand =>
            _QGUpCommand ?? (_QGUpCommand = new DelegateCommand<string>(ExecuteQGUpCommand));

        void ExecuteQGUpCommand(string parameter)
        {
            switch (parameter)
            {
                case "ST4压料上下气缸":
                    ST04PushMaterialUpDownCy.cylinderPCToPLC_Global.bManualBT =false;
                    break;

                case "ST4电极上下气缸":
                    ST04ElectrodeUpDownCy.cylinderPCToPLC_Global.bManualBT = false;
                    break;
                case "ST4工站脱离":
                    ST04StationBreakAwayCy.cylinderPCToPLC_Global.bManualBT = false;
                    break;

                default:
                    break;
            }
        }


        private DelegateCommand _GoHomeDownCmd;
        public DelegateCommand GoHomeDownCmd =>
            _GoHomeDownCmd ?? (_GoHomeDownCmd = new DelegateCommand(ExecuteGoHomeDownCmd));

        void ExecuteGoHomeDownCmd()
        {
            LeftMotorPCToPLC_Global.GoHome = true;
        }


        private DelegateCommand _GoHomeUpCmd;
        public DelegateCommand GoHomeUpCmd =>
            _GoHomeUpCmd ?? (_GoHomeUpCmd = new DelegateCommand(ExecuteGoHomeUpCmd));

        void ExecuteGoHomeUpCmd()
        {
            LeftMotorPCToPLC_Global.GoHome = false;
        }


        private DelegateCommand _WorkDownPushDownCmd;
        public DelegateCommand WorkDownPushDownCmd =>
            _WorkDownPushDownCmd ?? (_WorkDownPushDownCmd = new DelegateCommand(ExecuteWorkDownPushDownCmd));

        void ExecuteWorkDownPushDownCmd()
        {
            LeftMotorPCToPLC_Global.Move = true;
        }

        private DelegateCommand _WorkDownPushUpCmd;
        public DelegateCommand WorkDownPushUpCmd =>
            _WorkDownPushUpCmd ?? (_WorkDownPushUpCmd = new DelegateCommand(ExecuteWorkDownPushUpCmd));

        void ExecuteWorkDownPushUpCmd()
        {
            LeftMotorPCToPLC_Global.Move = false;
        }

        private DelegateCommand _LeftAbortDownCmd;
        public DelegateCommand LeftAbortDownCmd =>
            _LeftAbortDownCmd ?? (_LeftAbortDownCmd = new DelegateCommand(ExecuteLeftAbortDownCmd));

        void ExecuteLeftAbortDownCmd()
        {
            LeftMotorPCToPLC_Global.Abort = true;
        }

        private DelegateCommand _LeftAbortUpCmd;
        public DelegateCommand LeftAbortUpCmd =>
            _LeftAbortUpCmd ?? (_LeftAbortUpCmd = new DelegateCommand(ExecuteLeftAbortUpCmd));

        void ExecuteLeftAbortUpCmd()
        {
            LeftMotorPCToPLC_Global.Abort = false;

        }

        private DelegateCommand _RightGoHomeDownCmd;
        public DelegateCommand RightGoHomeDownCmd =>
            _RightGoHomeDownCmd ?? (_RightGoHomeDownCmd = new DelegateCommand(ExecuteRightGoHomeDownCmd));

        void ExecuteRightGoHomeDownCmd()
        {
            RightMotorPCToPLC_Global.GoHome = true;
        }

        private DelegateCommand _RightGoHomeUpCmd;
        public DelegateCommand RightGoHomeUpCmd =>
            _RightGoHomeUpCmd ?? (_RightGoHomeUpCmd = new DelegateCommand(ExecuteRightGoHomeUpCmd));

        void ExecuteRightGoHomeUpCmd()
        {
            RightMotorPCToPLC_Global.GoHome = false;

        }

        private DelegateCommand _RightWorkDownPushDownCmd;
        public DelegateCommand RightWorkDownPushDownCmd =>
            _RightWorkDownPushDownCmd ?? (_RightWorkDownPushDownCmd = new DelegateCommand(ExecuteRightWorkDownPushDownCmd));

        void ExecuteRightWorkDownPushDownCmd()
        {
            RightMotorPCToPLC_Global.Move = true;
        }

        private DelegateCommand _RightWorkDownPushUpCmd;
        public DelegateCommand RightWorkDownPushUpCmd =>
            _RightWorkDownPushUpCmd ?? (_RightWorkDownPushUpCmd = new DelegateCommand(ExecuteRightWorkDownPushUpCmd));

        void ExecuteRightWorkDownPushUpCmd()
        {
            RightMotorPCToPLC_Global.Move = false;

        }

        private DelegateCommand _RightAbortDownCmd;
        public DelegateCommand RightAbortDownCmd =>
            _RightAbortDownCmd ?? (_RightAbortDownCmd = new DelegateCommand(ExecuteRightAbortDownCmd));

        void ExecuteRightAbortDownCmd()
        {
            RightMotorPCToPLC_Global.Abort = true;

        }

        private DelegateCommand _RightAbortUpCmd;
        public DelegateCommand RightAbortUpCmd =>
            _RightAbortUpCmd ?? (_RightAbortUpCmd = new DelegateCommand(ExecuteRightAbortUpCmd));

        void ExecuteRightAbortUpCmd()
        {

            RightMotorPCToPLC_Global.Abort = false;
        }


        private DelegateCommand _LoadWireManDownCmd;
        public DelegateCommand LoadWireManDownCmd =>
            _LoadWireManDownCmd ?? (_LoadWireManDownCmd = new DelegateCommand(ExecuteLoadWireManDownCmd));

        void ExecuteLoadWireManDownCmd()
        {
            WirePCToPLC_Global.LoadWireManBT = true;

        }

        private DelegateCommand _LoadWireManUpCmd;
        public DelegateCommand LoadWireManUpCmd =>
            _LoadWireManUpCmd ?? (_LoadWireManUpCmd = new DelegateCommand(ExecuteLoadWireManUpCmd));

        void ExecuteLoadWireManUpCmd()
        {
            WirePCToPLC_Global.LoadWireManBT = false;
        }


        private DelegateCommand _WeldStartDownCmd;
        public DelegateCommand WeldStartDownCmd =>
            _WeldStartDownCmd ?? (_WeldStartDownCmd = new DelegateCommand(ExecuteWeldStartDownCmd));

        void ExecuteWeldStartDownCmd()
        {
            WirePCToPLC_Global.WeldStartBT = true;
        }

        private DelegateCommand _WeldStartUpCmd;
        public DelegateCommand WeldStartUpCmd =>
            _WeldStartUpCmd ?? (_WeldStartUpCmd = new DelegateCommand(ExecuteWeldStartUpCmd));

        void ExecuteWeldStartUpCmd()
        {
            WirePCToPLC_Global.WeldStartBT = false;

        }

        private DelegateCommand _WorkPosSearchDownCmd;
        public DelegateCommand WorkPosSearchDownCmd =>
            _WorkPosSearchDownCmd ?? (_WorkPosSearchDownCmd = new DelegateCommand(ExecuteWorkPosSearchDownCmd));

        void ExecuteWorkPosSearchDownCmd()
        {
            WirePCToPLC_Global.WorkPosSearchBT = true;
        }

        private DelegateCommand _WorkPosSearchUpCmd;
        public DelegateCommand WorkPosSearchUpCmd =>
            _WorkPosSearchUpCmd ?? (_WorkPosSearchUpCmd = new DelegateCommand(ExecuteWorkPosSearchUpCmd));

        void ExecuteWorkPosSearchUpCmd()
        {
            WirePCToPLC_Global.WorkPosSearchBT = false;

        }
        #endregion


        #region 属性


        private ObservableCollection<CylinderModel> _QGList;
        public ObservableCollection<CylinderModel> QGList
        {
            get { return _QGList ?? (_QGList = new ObservableCollection<CylinderModel>()); }
            set { SetProperty(ref _QGList, value); }
        }


        private bool _LoadWireManStart;
        public bool LoadWireManStart
        {
            get { return _LoadWireManStart; }
            set { SetProperty(ref _LoadWireManStart, value); }
        }

        private bool _WeldStart;
        public bool WeldStart
        {
            get { return _WeldStart; }
            set { SetProperty(ref _WeldStart, value); }
        }

        private string _LoadWireManStartText;
        public string LoadWireManStartText
        {
            get { return _LoadWireManStartText; }
            set { SetProperty(ref _LoadWireManStartText, value); }
        }

        private string _WeldStartText;
        public string WeldStartText
        {
            get { return _WeldStartText; }
            set { SetProperty(ref _WeldStartText, value); }
        }


        private float _TargetPos_X;
        public float TargetPos_X
        {
            get { return _TargetPos_X; }
            set { SetProperty(ref _TargetPos_X, value); }
        }

        private float _TargetPos_YR;
        public float TargetPos_YR
        {
            get { return _TargetPos_YR; }
            set { SetProperty(ref _TargetPos_YR, value); }
        }

        private float _TargetPos_YL;
        public float TargetPos_YL
        {
            get { return _TargetPos_YL; }
            set { SetProperty(ref _TargetPos_YL, value); }
        }

        private int _WeldLength;
        public int WeldLength
        {
            get { return _WeldLength; }
            set { SetProperty(ref _WeldLength, value); }
        }

        private float _ActualTension;
        public float ActualTension
        {
            get { return _ActualTension; }
            set { SetProperty(ref _ActualTension, value); }
        }
        #endregion
    }
}
